package com.lovefreedom.psi.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class Utils {

    //生成指定长度的字符串,即生成strLong个str字符串
    public static String StringOfChar(int strLong, String str)
    {
        String ReturnStr = "";
        for (int i = 0; i < strLong; i++)
        {
            ReturnStr += str;
        }
        return ReturnStr;
    }
    
    /// <summary>
    /// URL字符解码
    /// </summary>
    public static String UrlDecode(String str)
    {
        if (str.isEmpty())
        {
            return "";
        }
        //return HttpContext.Current.Server.UrlDecode(str);
        return str;
    }   
    
    
    /// <summary>
    /// 组合URL参数
    /// </summary>
    /// <param name="_url">页面地址</param>
    /// <param name="_keys">参数名称</param>
    /// <param name="_values">参数值</param>
    /// <returns>String</returns>
    /*
    public static String CombUrlTxt(String _url, String _keys, params String[] _values)
    {
        StringBuilder urlParams = new StringBuilder();
        try
        {
            string[] keyArr = _keys.Split(new char[] { '&' });
            for (int i = 0; i < keyArr.Length; i++)
            {
                if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
                {
                    _values[i] = UrlEncode(_values[i]);
                    urlParams.Append(string.Format(keyArr[i], _values) + "&");
                }
            }
            if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
                urlParams.Insert(0, "?");
        }
        catch
        {
            return _url;
        }
        return _url + DelLastChar(urlParams.ToString(), "&");
    }   
    */ 
    /// <summary>
    /// 替换指定的字符串
    /// </summary>
    /// <param name="originalStr">原字符串</param>
    /// <param name="oldStr">旧字符串</param>
    /// <param name="newStr">新字符串</param>
    /// <returns></returns>
    public static String ReplaceStr(String originalStr, String oldStr, String newStr)
    {
        if (oldStr.isEmpty())
        {
            return "";
        }
        return originalStr.replace(oldStr, newStr);
    }    
    
    /// <summary>
    /// 返回分页页码
    /// </summary>
    /// <param name="pageSize">页面大小</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="totalCount">总记录数</param>
    /// <param name="linkUrl">链接地址，__id__代表页码</param>
    /// <param name="centSize">中间页码数量</param>
    /// <returns></returns>
    public static String OutPageList(int pageSize, int pageIndex, int totalCount, String linkUrl, int centSize)
    {
        //计算页数
        if (totalCount < 1 || pageSize < 1)
        {
            return "";
        }
        int pageCount = totalCount / pageSize;
        if (pageCount < 1)
        {
            return "";
        }
        if (totalCount % pageSize > 0)
        {
            pageCount += 1;
        }
        if (pageCount <= 1)
        {
            return "";
        }
        StringBuilder pageStr = new StringBuilder();
        String pageId = "__id__";
        String firstBtn = "<a href=\"" + ReplaceStr(linkUrl, pageId, ((pageIndex - 1)+"")) + "\">上一页</a>";
        String lastBtn = "<a href=\"" + ReplaceStr(linkUrl, pageId, ((pageIndex + 1)+"")) + "\">下一页</a>";
        String firstStr = "<a href=\"" + ReplaceStr(linkUrl, pageId, "1") + "\">1</a>";
        String lastStr = "<a href=\"" + ReplaceStr(linkUrl, pageId, (pageCount+"")) + "\">" + pageCount + "</a>";

        if (pageIndex <= 1)
        {
            firstBtn = "<span class=\"disabled\">上一页</span>";
        }
        if (pageIndex >= pageCount)
        {
            lastBtn = "<span class=\"disabled\">下一页</span>";
        }
        if (pageIndex == 1)
        {
            firstStr = "<span class=\"current\">1</span>";
        }
        if (pageIndex == pageCount)
        {
            lastStr = "<span class=\"current\">" + pageCount + "</span>";
        }
        int firstNum = pageIndex - (centSize / 2); //中间开始的页码
        if (pageIndex < centSize)
            firstNum = 2;
        int lastNum = pageIndex + centSize - ((centSize / 2) + 1); //中间结束的页码
        if (lastNum >= pageCount)
            lastNum = pageCount - 1;
        pageStr.append(firstBtn + firstStr);
        if (pageIndex >= centSize)
        {
            pageStr.append("<span>...</span>\n");
        }
        for (int i = firstNum; i <= lastNum; i++)
        {
            if (i == pageIndex)
            {
                pageStr.append("<span class=\"current\">" + i + "</span>");
            }
            else
            {
                pageStr.append("<a href=\"" + ReplaceStr(linkUrl, pageId, i+"") + "\">" + i + "</a>");
            }
        }
        if (pageCount - pageIndex > centSize - ((centSize / 2)))
        {
            pageStr.append("<span>...</span>");
        }
        pageStr.append(lastStr + lastBtn);
        return pageStr.toString();
    }

    /**
     * 根据名字获取cookie
     * @param request
     * @param name cookie名字
     * @return
     */
    public static Cookie getCookieByName(HttpServletRequest request,String name){
        Map<String,Cookie> cookieMap = ReadCookieMap(request);
        if(cookieMap.containsKey(name)){
            Cookie cookie = (Cookie)cookieMap.get(name);
            return cookie;
        }else{
            return null;
        }   
    }
          
    /**
     * 将cookie封装到Map里面
     * @param request
     * @return
     */
    private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){  
        Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
        Cookie[] cookies = request.getCookies();
        if(null!=cookies){
            for(Cookie cookie : cookies){
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }    
    
}
    
    

    
    
